package com.kronos.gamestates;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Application.ApplicationType;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.kronos.audio.MusicStream;
import com.kronos.game.KronosGame;
import com.kronos.game.ParallaxBackground;
import com.kronos.game.ParallaxLayer;
import com.kronos.gui.Container;
import com.kronos.gui.ImageFontButton;
import com.kronos.gui.ImageFontField;
import com.kronos.managers.GameStateManager;

public class LoginState extends GameState {
	private ImageFontButton button;
	private ImageFontField user, pass;
	private Container container;
	
	private Texture bg, clouds;
	private ParallaxLayer layer1, layer2;
	private ParallaxBackground background;
	
	public LoginState(GameStateManager gsm) {
		super(gsm);
		
		bg = KronosGame.manager.getTexture("parallax.png");
		clouds = KronosGame.manager.getTexture("clouds.png");
		layer1 = new ParallaxLayer(bg, 0, 0.1f, 0.0f);
		layer2 = new ParallaxLayer(clouds, 0, 0.1f, 0.0f);
		ParallaxLayer[] layers = {layer1, layer2};
		background = new ParallaxBackground(layers, cam);
		
		canvas.addDrawable(container = new Container(KronosGame.V_WIDTH/2 - (300/2), KronosGame.V_HEIGHT/2 - (200/2), cam));
		canvas.addDrawable(button = new ImageFontButton("Login", container.getX() + 100, container.getY() + 12, cam));
	    canvas.addDrawable(user = new ImageFontField(container.getX() + 100, container.getY() + 115, cam));	    
	    canvas.addDrawable(pass = new ImageFontField(container.getX() + 100, container.getY() + 72, cam));
	    pass.setPasswordField(true);
	    
	    if(MusicStream.isPlaying("intro")) {
			MusicStream.stopSound("intro");
			MusicStream.disposeSound("intro");
		}
		MusicStream.playSound("game");
		MusicStream.loopSound("game", true);
	}

	@Override
	public void update() { 
		/*if(button.changedState()) {
			canvas.clearAll();
			if(Gdx.app.getType() == ApplicationType.Android) {
				Gdx.input.setOnscreenKeyboardVisible(false);
			} 
			gsm.setState(GameStateManager.PLAYING_STATE);
			
		}*/
		background.scrollRightX(0.5f* Gdx.graphics.getDeltaTime());
	}
	
	@Override
	public void render() { 
		sb.begin();
		sb.setColor(1.0f, 1.0f, 1.0f, 1);
		Gdx.gl.glClearColor(91.0f/255.0f, 172.0f/255.0f, 245.0f/255.0f, 1.0f);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		background.render(sb); 
		sb.end();       
		
		canvas.drawDrawables(container, button, user, pass); 
	}

	@Override
	public void dispose() {
		canvas.clearAll();
		sb.dispose();
		if(MusicStream.isPlaying("intro")) {
			MusicStream.disposeSound("intro");
		}
	}
}
